//获取应用实例
const Page = require('../../utils/ald-stat.js').Page;
const app = getApp()
const util = require('../../utils/util.js')
const time = require('../../utils/time.js')
const Debug = require('../../utils/Debug.js')
import {
        UserModel
} from '../../models/user.js'
let user = new UserModel()
Page({
        data: {
                pages: 1, //默认第一页
                pageSize: 10, //每页10个
                share_user: "", //分享用户
                navactive: 0, //默认最热视频
                jiazai: false, //加载
                channel: "", //渠道
                yaoqingshow: true, //是否显示邀请
                navlist: ["最热", "推荐", "最新"],
                list0: [],
                list1: [],
                list2: [],
                adda: '', //是否加广告
                addb: '', //是否加广告
                addc: '', //是否加广告
                text: ["@所有人，以后就来这里听秦腔吧，专业听戏用这一个就够了", "这里的秦腔很全哦！快来和我一起听戏吧！", "很全很专业的秦腔小程序，一起听戏吧！"],
                loginshow: false, //是否显示授权提示框
                toastshow:false,//桌面快捷任务
        },
        onLoad: function(options) {
                var that = this
                this.request()
                that.setData({
                        headerheight: app.globalData.navHeight,
                        windowHeight: app.globalData.windowHeight,
                })
                
                //     渠道
                if (!(options.channel == undefined)) {
                        that.setData({
                                channel: options.channel
                        })
                }
                //     判断登录

                if (wx.getStorageSync("tekon") == "") {
                        that.login()
                }
                // 判断入口
                if (!(options.user_id == undefined)) {
                        that.setData({
                                share_user: options.user_id
                        })
                }
                   //  添加到桌面入口
                if (app.globalData.scene == 1023){
                        that.entrance()
                }
                //如果用户没同意授权 则每次进首页都会弹出提示框
                wx.getSetting({
                        success: res => {
                                if (typeof(res.authSetting['scope.userInfo']) == 'undefined') {
                                        this.setData({
                                                loginshow: true
                                        })
                                }
                        }
                })
        },
        //   安卓添加到桌面
        entrance: function () {
                var header = {
                        'authorization': wx.getStorageSync("tekon"),
                        'content-type': "application/x-www-form-urlencoded"
                }
                var data = {
                        missionId: 13
                }
                util.formhttp(app.globalData.appurl + "/mission", 'POST', data, header, (res) => {
                        console.log(res)
                        var that = this
                           if (res.data.missionAccomplished == 1) {
                                that.setData({
                                        toastshow: true,
                                        contens: "奖励添加桌面的你",
                                        jinbi: res.data.missionReward
                                })
                                setTimeout(function () {
                                        that.setData({
                                                toastshow: false
                                        })
                                }, 3000)
                        }
                })
        },
        // 向下滑
        lower: function(e) {
                console.log(e)
                var that = this
                var pages = that.data.pages
                pages++
                that.setData({
                        pages: pages
                })
                that.request()
        },
        // 登录
        login: function() {
                var that = this
                wx.login({
                        success: function(res) {
                                var code = res.code
                                // console.log(that.data.channel)
                                console.log(that.data.share_user)
                                var data = {
                                        userCode: code,
                                        channel: that.data.channel,
                                        userImageUrl: "",
                                        userName: "",
                                        promoterId: that.data.share_user
                                }
                                util.promotion(app.globalData.appurl + "/qinqiang/user", "POST", data, that.succeed)
                        }
                })
        },
        succeed: function(res) {
                // console.log(res)
                Debug.debug("<-------登录信息------->")
                Debug.debug(res)
                wx.setStorageSync('tekon', res.data.token)
                wx.setStorageSync('userInfo', res.data.user)
        },

        onShareAppMessage: function(res) {
                var that = this
                return {
                        title: that.data.text[Math.floor(Math.random() * 3)],
                        imageUrl: "https://img-1256481299.cos.ap-guangzhou.myqcloud.com/qqhxcx/sa.jpg",
                        path: '/pages/index/index?&user_id=' + wx.getStorageSync("userInfo").id,
                }
        },
        //邀请有礼
        yaoqing: function() {
                wx.navigateTo({
                        url: '/pages/invite/invite',
                })
        },
        quxiao: function() {
                this.setData({
                        yaoqingshow: false
                })
        },
        // 重新加载
        jiazai: function(e) {
                var that = this
                this.setData({
                        navactive: e.currentTarget.dataset.index,
                        pages: 1,
                        ist0: [],
                        list1: [],
                        list2: [],
                })
                this.login()
                setTimeout(function() {
                        that.request()
                }, 500)

        },
        // 点击切换导航
        navactive: function(e) {
                //     console.log('点击')
                this.setData({
                        navactive: e.currentTarget.dataset.index
                })
                // this.request()
        },
        //滑动 切换导航
        bindChange: function(e) {
                //     console.log('滑动')
                this.setData({
                        navactive: e.detail.current,
                        scrollTop: 0
                })

                if (this.data.list0.length == 0) {
                        this.request()
                } else if (this.data.list1.length == 0) {
                        this.request()
                } else if (this.data.list2.length == 0) {
                        this.request()
                }
        },

        // 列表请求
        request: function() {
                var that = this
                wx.showNavigationBarLoading()
                wx.showLoading({
                        title: '努力加载中...',
                })
                var data = {
                        pageIndex: that.data.pages,
                        pageSize: that.data.pageSize
                };
                var header = {
                        'authorization': wx.getStorageSync("tekon"),
                        'content-type': "application/x-www-form-urlencoded"
                }
                if (that.data.navactive == 0) {
                        // 最热
                        util.formhttp(app.globalData.appurl + "/qinqiang/popularVideos", 'GET', data, header, that.porcessData);
                } else if (that.data.navactive == 1) {
                        // 推荐
                        util.formhttp(app.globalData.appurl + "/qinqiang/recommendVideos", 'GET', data, header, that.porcessData);
                } else if (that.data.navactive == 2) {
                        // 最新
                        util.formhttp(app.globalData.appurl + "/qinqiang/newVideos", 'GET', data, header, that.porcessData);
                }
        },
        //列表成功的处理
        porcessData: function(res) {
                Debug.debug("<------- 最热,推荐,最新,请求列表------->")
                Debug.debug(res)
                var that = this
                // if (res.status == 401){

                // }
                if (res.code == 1) {
                        that.setData({
                                jiazai: true
                        })
                } else {
                        that.setData({
                                jiazai: false
                        })
                }
                wx.hideNavigationBarLoading()
                wx.hideLoading()
                if (this.data.navactive == 0) {
                        var lista = that.data.list0
                        for (var ids in res.data) {
                                var temp = {
                                        id: res.data[ids].id,
                                        title: res.data[ids].title,
                                        imgUrl: res.data[ids].imgUrl,
                                        hits: res.data[ids].hits,
                                        duration: time.formatTime(new Date(res.data[ids].duration)),
                                        authorimg: res.data[ids].userImgUrl,
                                        authorname: res.data[ids].userName,
                                        types: res.data[ids].sourceType
                                }
                                lista.push(temp)
                        }
                        if (this.data.adda == '') {
                                var ad = {
                                        adunit: 'adunit-65a26ae46b194904',
                                        show: true
                                }
                                lista.splice(1, 0, ad);
                        }
                        if (res.data.length == 0) {
                                wx.showToast({
                                        title: "无更多内容",
                                        icon: 'none',
                                        duration: 2000
                                })
                        }
                        this.setData({
                                list0: lista,
                                adda: "a"
                        })
                } else if (this.data.navactive == 1) {
                        if (res.status == 401) {
                                that.login()
                                that.request()
                        }
                        var listb = that.data.list1
                        for (var ids in res.data) {
                                var temp = {
                                        id: res.data[ids].id,
                                        title: res.data[ids].title,
                                        imgUrl: res.data[ids].imgUrl,
                                        hits: res.data[ids].hits,
                                        duration: time.formatTime(new Date(res.data[ids].duration)),
                                        authorimg: res.data[ids].userImgUrl,
                                        authorname: res.data[ids].userName,
                                        types: res.data[ids].sourceType
                                }
                                listb.push(temp)
                        }
                        if (this.data.addb == '') {
                                var ad = {
                                        adunit: 'adunit-65a26ae46b194904',
                                        show: true
                                }
                                listb.splice(1, 0, ad);
                        }
                        if (res.data.length == 0) {
                                wx.showToast({
                                        title: "无更多内容",
                                        icon: 'none',
                                        duration: 2000
                                })
                        }
                        this.setData({
                                list1: listb,
                                addb: "b"
                        })

                } else if (this.data.navactive == 2) {
                        var listc = that.data.list2
                        for (var ids in res.data) {
                                var temp = {
                                        id: res.data[ids].id,
                                        title: res.data[ids].title,
                                        imgUrl: res.data[ids].imgUrl,
                                        hits: res.data[ids].hits,
                                        duration: time.formatTime(new Date(res.data[ids].duration)),
                                        authorimg: res.data[ids].userImgUrl,
                                        authorname: res.data[ids].userName,
                                        types: res.data[ids].sourceType
                                }
                                listc.push(temp)
                        }
                        if (this.data.addc == '') {
                                var ad = {
                                        adunit: 'adunit-65a26ae46b194904',
                                        show: true
                                }
                                listc.splice(1, 0, ad);
                        }
                        if (res.data.length == 0) {
                                wx.showToast({
                                        title: "无更多内容",
                                        icon: 'none',
                                        duration: 2000
                                })
                        }
                        this.setData({
                                list2: listc,
                                addc: "c"
                        })

                }
        }
})